package com.fwzc.giuseppe.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.adapter.FragmentPageAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.fragment.WaitPaymentFragment;
import com.fwzc.giuseppe.fragment.WaitReceiveFragment;

public class UnReceiveActivity extends BaseActivity implements OnClickListener,
		OnPageChangeListener {
	private TextView wait_payment, wait_receive;
	private ViewPager viewPage;
	private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
	private View slide_rod;
	private WindowManager manager;
	private int width, tabLength, currIndex;
	private FragmentPageAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_unreceived);

		initBar();
		initView();
		initViewPage();
	}

	private void initView() {
		actionbar_side_name.setText("未收货");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);

		wait_payment = (TextView) findViewById(R.id.wait_payment);
		wait_receive = (TextView) findViewById(R.id.wait_receive);
		viewPage = (ViewPager) findViewById(R.id.match_content);
		slide_rod = (View) findViewById(R.id.slide_rod);
		wait_payment.setSelected(true);
		wait_payment.setOnClickListener(this);
		wait_receive.setOnClickListener(this);

		// 获取屏幕宽度，初始化滑块的宽度
		DisplayMetrics metrics = new DisplayMetrics();
		manager = this.getWindowManager();
		manager.getDefaultDisplay().getMetrics(metrics);
		width = metrics.widthPixels;
		tabLength = width / 2;
		LayoutParams lp = slide_rod.getLayoutParams();
		lp.width = tabLength;
		slide_rod.setLayoutParams(lp);
	}

	private void initViewPage() {
		WaitPaymentFragment paymentFragment = new WaitPaymentFragment();
		WaitReceiveFragment receiveFragment = new WaitReceiveFragment();
		fragmentList.add(paymentFragment);
		fragmentList.add(receiveFragment);
		adapter = new FragmentPageAdapter(getSupportFragmentManager(),
				fragmentList);
		viewPage.setAdapter(adapter);
		viewPage.setCurrentItem(0);
		viewPage.setOnPageChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		// 待付款
		case R.id.wait_payment:
			viewPage.setCurrentItem(0);
			break;

		// 待收货
		case R.id.wait_receive:
			viewPage.setCurrentItem(1);
			break;
		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case 0:
			wait_payment.setSelected(true);
			wait_receive.setSelected(false);
			break;
			
		case 1:
			wait_payment.setSelected(false);
			wait_receive.setSelected(true);
			break;
		default:
			break;
		}
		Animation animation = new TranslateAnimation(tabLength * currIndex,
				tabLength * position, 0, 0);
		currIndex = position;
		animation.setFillAfter(true);
		animation.setDuration(300);
		slide_rod.startAnimation(animation);
	}
}
